package com.luzhikun.cookieTest; /**
 * ClassName: ${NAME}
 * Description: 在客户端生成Cookie
 * date: 2021/4/29 19:31
 *
 * @author luzhikun
 * @version 1.1
 * @since JDK 1.8
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        // 获取请求域属性的值
////        String username = request.getParameter("username");
////        String password = request.getParameter("password");
////        System.out.println(username);
////        System.out.println(password);
////        // 创建两个Cookie
////        Cookie cookie=new Cookie("company", username);
////        Cookie cookie1=new Cookie("address", password);
//
//        // http://localhost:8080/15_cookie/xxx/ooo/cookieServlet
//
//        // 指定Cookie绑定的路径
//        // cookie.setPath(request.getContextPath()+"/xxx/ooo/cookieServlet/aaa");
//        // cookie1.setPath(request.getContextPath()+"/bbb");

//        // 设置Cookie的有效值,这个值为一个整数型，单位为秒
//        // 该值大于0,表示将Cookie存放在客户端的硬盘中
//        // 该值小于0，与不设置效果相同，会将Cookie存放在浏览器的缓存中
//        // 该值等于0，表示Cookie一旦生成，立刻失效
//        // cookie.setMaxAge( 60 * 60);// 设置Cookie的有效期为1小时
//        // cookie1.setMaxAge( 60 * 60 * 24 * 10);// 设置Cookie的有效期为10天
//        Cookie[] cookies = request.getCookies();

//        for (Cookie cookie2: cookies) {
//            System.out.println(cookie2.getName()+"==="+cookie2.getValue());
//        }
//
//        // 向响应中添加Cookie·
//        response.addCookie(cookie);
//        response.addCookie(cookie1);
//        System.out.println("-----------------------------------------------");
//        // response.sendRedirect(request.getContextPath()+"/xxx/ooo/reqCookie");
//        request.getRequestDispatcher("/xxx/ooo/reqCookie").forward(request,response);
//          doPost(request,response);
//          获取请求域属性的值


    }
//
//    }

    //    @Override
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String hobby = request.getParameter("hobby");

        System.out.println(hobby);
        // 创建两个Cookie

        Cookie cookie1 = new Cookie("address", hobby);

        Cookie[] cookies = request.getCookies();

        if (cookies != null && cookies[0].getValue().trim().length() > 0) {
            // 向响应中添加Cookie
            response.addCookie(cookie1);
            response.sendRedirect(request.getContextPath() + "/xxx/ooo/reqCookie");
            //          request.getRequestDispatcher("/xxx/ooo/reqCookie").forward(request,response);
        } else {
            System.out.println("这他妈就是空的");
        }
        System.out.println("-----------------------------------------------");
    }

}
